package com.passive.lock.controller.sys;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.passive.lock.model.vo.TMenuVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.MenuService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("me")
public class MenuController {
	@Autowired
	private MenuService menuService;

    public static final String DEMO_CACHE_NAME = "mytest";
    //这里的单引号不能少，否则会报错，被识别是一个对象;
    //public static final String CACHE_KEY = "'demoInfo'";
    @Cacheable(value=DEMO_CACHE_NAME,key="'aaaa'")
    public TUserVo findById(String token){
       System.err.println("没有走缓存！");
       return null;
    }
    
    @RequestMapping(value = "/menu")
	@ResponseBody
	public Result menuTree(HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		
		TUserVo aaa= this.findById("asdasdasdas");
		System.out.println(aaa);
		try {
			Result result = menuService.menuTree(tUserVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/menulist")
	@ResponseBody
	public Result menuList(@RequestParam String pcode, HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = menuService.menuList(pcode);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	@RequestMapping(value = "/addmenu")
	@ResponseBody
	public Result addMenu(@RequestBody TMenuVo tMenuVo) {
		try {
			Result result = menuService.addMenu(tMenuVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updatemenu")
	@ResponseBody
	public Result updateMenu(@RequestBody TMenuVo tMenuVo) {
		try {
			Result result = menuService.updateMenu(tMenuVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletemenu")
	@ResponseBody
	public Result deleteMenu(@RequestBody TMenuVo tMenuVo) {
		try {
			Result result = menuService.deleteMenu(tMenuVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
}
